package com.example.springboot.controller;

import com.example.springboot.common.Result;
import com.example.springboot.common.ResultCode;
import com.example.springboot.domain.po.Admin;
import com.example.springboot.domain.po.User;
import com.example.springboot.exception.CustomException;
import com.example.springboot.service.AdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author：伐柯梦姝
 * @Package：com.example.springboot.controller
 * @Project：springboot
 * @name：AdminController
 * @Date：2025/2/3 22:29
 * @Filename：AdminController
 * @for:
 */
@Api(tags = "管理员管理接口")
@RestController
@CrossOrigin
@Slf4j
@RequestMapping("/admin")
public class AdminController {
    @Resource
    private AdminService adminService;

    @ApiOperation("查询管理员接口")
    @GetMapping("/alldata")
    public Result getData(){
        List<Admin> adminList = adminService.getAll();
        if (adminList.size() < 1){
            throw new CustomException(ResultCode.DATA_ADMIN_LESS);
        }
        return Result.success(adminList);

    }

    @ApiOperation("新增管理员接口")
    @PostMapping
    public Result adminRegister(@RequestBody Admin admin){

        return Result.success(adminService.adminRegister(admin));
    }

    @ApiOperation("删除管理员接口")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        adminService.deleteById(id);
        return Result.success();
    }

    @ApiOperation("修改管理员接口")
    @PutMapping
    public Result update(@RequestBody Admin admin){
        adminService.update(admin);
        return Result.success(admin);
    }

    @ApiOperation("分页查询管理员接口")
    @PostMapping("/page")
    public Result page(@RequestBody Admin search,
                       @RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "5") Integer pageSize){
        return Result.success(adminService.findPage(search, pageNum, pageSize));
    }


    @ApiOperation("修改角色权限接口")
    @PostMapping("/changeRole")
    public Result adminRole(@RequestBody Admin admin){

        return Result.success(adminService.adminRole(admin));

    }


}
